package me.numark;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.fragments.DiscoverFragment;
import me.numark.fragments.NewsFeedFragment;
import me.numark.fragments.ProfileFragment;
import me.numark.utils.Global;
import me.numark.utils.Settings;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    @InjectView(R.id.tab_feed)
    ImageView tab_feed;

    @InjectView(R.id.tab_discover)
    ImageView tab_discover;

    @InjectView(R.id.tab_recommend)
    ImageView tab_recommend;

    @InjectView(R.id.tab_notice)
    ImageView tab_notice;

    @InjectView(R.id.tab_profile)
    ImageView tab_profile;

    private int currentFragmentIndex;
    private SparseArray<Fragment> fragments = new SparseArray<Fragment>(4);
    private ImageView[] tabButtons = new ImageView[4];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        appInit();
        uiInit();
        if (!Settings.isLoggedIn()) {
            startActivity(new Intent(this, LogInActivity.class));
            finish();
            return;
        }
        switchFragment(0, true);
    }

    void appInit() {
        MobclickAgent.updateOnlineConfig(this);
        PushManager.startWork(this, PushConstants.LOGIN_TYPE_API_KEY, "oCFrxubM4tS8ggIiEoDU2hhc");
        UmengUpdateAgent.update(this);
        if (Global.DeviceUniqueID == null) {
            Global.DeviceUniqueID = android.provider.Settings.Secure.getString(getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
            if (Global.DeviceUniqueID == null) {
                Global.DeviceUniqueID = String.valueOf(System.currentTimeMillis() % 6000);
            }
        }
    }

    void uiInit() {
        tab_discover.setOnClickListener(this);
        tab_feed.setOnClickListener(this);
        tab_notice.setOnClickListener(this);
        tab_profile.setOnClickListener(this);
        tab_recommend.setOnClickListener(this);
        tabButtons[0] = tab_feed;
        tabButtons[1] = tab_discover;
        tabButtons[2] = tab_notice;
        tabButtons[3] = tab_profile;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_feed:
                switchFragment(0, false);
                break;
            case R.id.tab_discover:
                switchFragment(1, false);
                break;
            case R.id.tab_notice:
                switchFragment(2, false);
                break;
            case R.id.tab_profile:
                switchFragment(3, false);
                break;
        }
    }

    private void switchFragment(int id, boolean force) {
        if (currentFragmentIndex == id && !force) {
            return;
        }
        tabButtons[currentFragmentIndex].setSelected(false);
        Fragment oldFragment = fragments.get(currentFragmentIndex);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (oldFragment != null)
            ft.hide(oldFragment);

        currentFragmentIndex = id;
        tabButtons[currentFragmentIndex].setSelected(true);
        Fragment newFragment = fragments.get(currentFragmentIndex);
        if (newFragment == null) {
            Bundle args = new Bundle();
            switch (currentFragmentIndex) {
                case 0:
                    newFragment = Fragment.instantiate(this, NewsFeedFragment.class.getName(),
                            args);
                    break;
                case 1:
                    newFragment = Fragment.instantiate(this, DiscoverFragment.class.getName(),
                            args);
                    break;
                case 3:
                    newFragment = Fragment.instantiate(this, ProfileFragment.class.getName(),
                            args);
                    break;
            }
            fragments.put(currentFragmentIndex, newFragment);
        }
        if (newFragment != null) {
            if (!newFragment.isAdded()) {
                ft.add(R.id.fragments, newFragment);
            }
            ft.show(newFragment);
        }
        ft.commit();
    }

/*
    @InjectView(R.id.msg_badge)
    TextView msgBadge;

    @InjectView(R.id.radio_left)
    RadioButton radioLeft;

    @InjectView(R.id.radio_right)
    RadioButton radioRight;

    @InjectView(R.id.radio_group)
    RadioGroup radioGroup;


    @InjectView(R.id.avatar)
    CircleImageView avatar;

    @InjectView(R.id.msg_view)
    ImageView msgView;

    private Fragment myFeedFragment, hotArticlesFragment;

    private SparseArray<Fragment> fragments = new SparseArray<Fragment>(2);

    private int currentFragmentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        appInit();
        if (!Settings.isLoggedIn()) {
            startActivity(new Intent(this, LogInActivity.class));
            finish();
        }
        avatar.setOnClickListener(this);
        msgView.setOnClickListener(this);
        bindMsgView();
        Bundle args = new Bundle();
        if (fragments.size() < 2) {
            args.putInt(NewsFeedFragment.TYPE, NewsFeedFragment.TYPE_MINE);
            myFeedFragment = Fragment.instantiate(this, NewsFeedFragment.class.getName(),
                    args);
            hotArticlesFragment = Fragment.instantiate(this, NewsListFragment.class.getName(),
                    args);
            fragments.put(R.id.radio_left, myFeedFragment);
            fragments.put(R.id.radio_right, hotArticlesFragment);
        }
        getFragmentManager().beginTransaction().add(R.id.fragments, myFeedFragment).show(
                myFeedFragment).commit();

        currentFragmentIndex = R.id.radio_left;
        radioGroup.setOnCheckedChangeListener(this);
    }

    private void bindMsgView() {
        int count = MessageUtils.getUnreadCount();
        if (count == 0) {
            msgView.setImageResource(R.drawable.ic_msg_read);
            msgBadge.setVisibility(View.GONE);
        } else if (count < 99) {
            msgView.setImageResource(R.drawable.ic_msg_read);
            msgBadge.setVisibility(View.VISIBLE);
            msgBadge.setText(Integer.toString(count));
        } else {
            msgView.setImageResource(R.drawable.ic_msg_unread);
            msgBadge.setVisibility(View.GONE);
        }
    }

    private void bindAvatarView() {
        if (!TextUtils.isEmpty(Settings.getAvatar())) {
            Picasso.with(this).load(Settings.getAvatar()).into(avatar);
        }
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.avatar:
                break;
            case R.id.msg_view:
                break;
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        if (!Settings.isLoggedIn()) {
            startActivity(new Intent(this, LogInActivity.class));
            finish();
        } else {
            bindMsgView();
            bindAvatarView();
        }
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switchFragment(checkedId, false);
    }

    private void switchFragment(int id, boolean force) {
        if (currentFragmentIndex == id && !force) {
            return;
        }
        Fragment fragment1 = fragments.get(id);
        Fragment fragment2 = (fragment1 == myFeedFragment) ? hotArticlesFragment
                : myFeedFragment;
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        if (!fragment1.isAdded()) {
            ft.add(R.id.fragments, fragment1);
        }
        ft.hide(fragment2).show(fragment1).commit();

        currentFragmentIndex = id;
    }
    */
}
